package com.juzipi.demo.config;


import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean，第三步
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(getDefaultWebSecurityManager());
        //添加shiro的内置过滤器
        /*
        anon：无需认证 就可以访问
        authc：必须认证 才能访问
        user：必须拥有 记住我 才能访问
        perms：拥有某个资源的权限才能访问
        role：拥有某个角色权限才能访问
         */
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("/user/add","authc");
        linkedHashMap.put("/user/update","authc");


        bean.setFilterChainDefinitionMap(linkedHashMap);
        //登录的请求
        bean.setLoginUrl("/tologin");

        return bean;

    }

    //DefaultWebSecurityManager，第二步
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm());//可以直接调用吧 userRealm()
        return securityManager;
    }


//    @Bean
//    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        //关联UserRealm
//        securityManager.setRealm(userRealm);//可以直接调用吧 userRealm()
//        return securityManager;
//    }

    //创建 realm 对象,第一步
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }



}
